from ddbtools import create_table, DbColumn, create_db
from gypb.db import get_db
from xqdata.constant import Frequency
from xqdata_ddb import init_db,add_object

def initilize_db():
    with get_db() as session:
        init_db(session=session)
        add_object(session=session, obj_name="stock",attribute_frequency=Frequency.DAILY)
        add_object(session=session, obj_name="product",attribute_frequency=Frequency.DAILY)
        add_object(session=session, obj_name="manager",attribute_frequency=Frequency.DAILY)
        add_object(session=session, obj_name="position",attribute_frequency=Frequency.DAILY,code_dtype="SYMBOL",obj_dtype="LONG")
        add_object(session=session, obj_name="dualcode",attribute_frequency=Frequency.DAILY,code_dtype="SYMBOL",obj_dtype="SYMBOL")
        # 在init_db中已经创建了dfs://infos attribute表
        # create_db(
        #     session=session,
        #     dbname="dfs://infos",
        #     partition_plan="RANGE(2000.01M + (0..30)*12)",
        #     engine="OLAP",
        # )

        # attribute_info_cols = [
        #     DbColumn("type", "SYMBOL", "类别"),
        #     DbColumn("frequency", "SYMBOL", "数据频率"),
        #     DbColumn("attribute", "SYMBOL", "属性名"),
        #     DbColumn("name", "SYMBOL", "属性注释"),
        #     DbColumn("dtype", "SYMBOL", "数据类型"),
        #     DbColumn("code_dtype", "SYMBOL", "索引列类型"),
        #     DbColumn("db_name", "SYMBOL", "数据库名"),
        #     DbColumn("table_name", "SYMBOL", "表名"),
        # ]
        # create_table(
        #     session=session,
        #     db_name="dfs://infos",
        #     table_name="attribute",
        #     columns=attribute_info_cols,
        # )
        # 管理人池
        pool_cols = [
            DbColumn("code", "SYMBOL", "管理人备案编码"),
            DbColumn("pool_name", "SYMBOL", "池层级"),
            DbColumn("strategy", "SYMBOL", "池策略类型"),
            DbColumn("prod_code", "SYMBOL", "追踪产品备案编码"),
            DbColumn("file_dir", "SYMBOL", "项目资料"),
            DbColumn("re_due_diligence", "DATE", "持续尽调日期"),
            DbColumn("is_valid", "BOOL", "是否有效"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="manager_pool",
            columns=pool_cols,
        )

        # 代销项目表
        sale_project_cols = [
            DbColumn("project_id", "INT", "项目ID"),
            DbColumn("project_name", "SYMBOL", "项目名称"),
            DbColumn("manager_code", "SYMBOL", "管理人备案编码"),
            DbColumn("manager_name", "SYMBOL", "管理人名称"),
            DbColumn("issue_dept", "SYMBOL", "发起部门"),
            DbColumn("issue_dept_personnel", "SYMBOL", "项目对接人"),
            DbColumn("personnel", "SYMBOL", "项目经理"),
            DbColumn("sale_range", "SYMBOL", "代销范围"),
            DbColumn("prod_code", "SYMBOL", "产品备案编码"),
            DbColumn("prod_name", "SYMBOL", "产品名称"),
            DbColumn("strategy", "SYMBOL", "投资策略"),
            DbColumn("benchmark", "STRING", "业绩基准"),
            DbColumn("file_dir", "SYMBOL", "项目资料文件夹"),
            DbColumn("sale_date", "DATE", "代销上线日期", "delta"),
            DbColumn("unsale_date", "DATE", "代销下架日期", "delta"),
            DbColumn("risk_level", "SYMBOL", "风险等级"),
            DbColumn("open_date", "STRING", "开放日安排"),
            DbColumn("status", "SYMBOL", "项目状态"),
            DbColumn("create_date", "DATE", "项目创建日期", "delta"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="sale_projects",
            columns=sale_project_cols,
        )

        # 周月报设置表
        weekly_monthly_reports_cols = [
            DbColumn("prod_code", "SYMBOL", "产品代码"),
            DbColumn("prod_name", "SYMBOL", "产品名称"),
            DbColumn("manager_name", "SYMBOL", "管理人名称"),
            DbColumn("establish_date", "DATE", "成立日期", "delta"),
            DbColumn("strategy", "SYMBOL", "策略类型"),
            DbColumn("second_strategy", "SYMBOL", "二级策略类型"),
            DbColumn("benchmark", "SYMBOL", "对标基准"),
            DbColumn("sale_range", "SYMBOL", "代销范围"),
            DbColumn("weekly_report", "BOOL", "纳入周报"),
            DbColumn("monthly_report", "BOOL", "纳入月报"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="weekly_monthly_reports",
            columns=weekly_monthly_reports_cols,
        )
        # 周月报策略顺序表
        weekly_monthly_reports_strategy_order_cols = [
            DbColumn("strategy", "SYMBOL", "策略类型"),
            DbColumn("strategy_order", "FLOAT", "策略顺序"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="weekly_monthly_reports_strategy_order",
            columns=weekly_monthly_reports_strategy_order_cols,
        )
        # 交易日表
        tradeday_cols = [
            DbColumn("datetime","DATE","日期","delta"),
            DbColumn("SSE","BOOL","上交所",),
            DbColumn("SZSE","BOOL","深交所",),
            DbColumn("CFFEX","BOOL","中金所",),
            DbColumn("SHFE","BOOL","上期所",),
            DbColumn("DCE","BOOL","大商所",),
            DbColumn("CZCE","BOOL","郑商所",),
            DbColumn("INE","BOOL","上海国际能源交易中心",),
            DbColumn("GFEX","BOOL","广交所",),
            DbColumn("BJSE","BOOL","北交所",),
            DbColumn("OF","BOOL","场外市场",),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="tradeday",
            columns=tradeday_cols,
        )
        # # 客户保有信息表
        # holdings_cols = [
        #     DbColumn("client_code", "LONG", "客户号"),
        #     DbColumn("prod_code", "SYMBOL", "产品代码"),
        #     DbColumn("datetime", "DATE", "日期","delta"),
        #     DbColumn("attribute", "SYMBOL", "属性名"),
        #     DbColumn("value", "DOUBLE", "值"),
        # ]
        # 火富牛产品信息表
        # product_cols = [
        #     DbColumn("code", "SYMBOL", "产品代码"),
        #     DbColumn("type", "SYMBOL", "产品类型"),
        #     DbColumn("name", "STRING", "最新产品名称"),
        #     DbColumn("shortname", "STRING", "最新产品简称"),
        #     DbColumn("manager_code", "SYMBOL", "管理人备案编码"),
        #     DbColumn("start_date", "DATE", "成立日期", "delta"),
        #     DbColumn("end_date", "DATE", "清算日期", "delta"),
        # ]
        # create_table(
        #     session=session,
        #     db_name="dfs://infos",
        #     table_name="product",
        #     columns=product_cols,
        # )
        hfn_product_cols = [
            DbColumn("fund_name", "SYMBOL", "产品名称"),
            DbColumn("fund_short_name", "SYMBOL", "产品简称"),
            DbColumn("register_number", "SYMBOL", "备案编码"),
            DbColumn("advisor", "SYMBOL", "投资顾问"),
            DbColumn("inception_date", "DATE", "成立日期", "delta"),
            DbColumn("liquidate_date", "DATE", "清算日期", "delta"),
            DbColumn("price_cw_nav", "DOUBLE", "复权净值"),
            DbColumn("price_nav", "DOUBLE", "最新净值"),
            DbColumn("price_cnw", "DOUBLE", "累计净值"),
            DbColumn("price_change", "DOUBLE", "涨跌幅"),
            DbColumn("price_date", "DATE", "最新净值日期", "delta"),
            DbColumn("puton_date", "DATE", "备案时间", "delta"),
            DbColumn("fund_type", "INT", "产品类型"),
            DbColumn("fund_state", "INT", "产品状态"),
            DbColumn("cycle_type", "INT", "净值更新周期"),
            DbColumn("mandator_name", "SYMBOL", "托管人"),
            DbColumn("strategy_one", "SYMBOL", "一级策略"),
            DbColumn("strategy_two", "SYMBOL", "二级策略"),
            DbColumn("strategy_three", "SYMBOL", "三级策略"),
            DbColumn("strategy_verify", "INT", "策略是否确认"),
            DbColumn("advisor2", "SYMBOL", "管理人"),
            DbColumn("amac_scale", "SYMBOL", "产品规模"),
            DbColumn("register_code", "SYMBOL", "登记编号"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="hfn_product",
            columns=hfn_product_cols,
        )
        # 产品-资金账户对照表
        prodcode_account_cols = [
            DbColumn("client_id", "LONG", "客户号"),
            DbColumn("account", "LONG", "资金账户"),
            DbColumn("prod_code", "SYMBOL", "产品代码"),
            DbColumn("prod_name", "SYMBOL", "产品名称"),
            DbColumn("prod_strategy", "SYMBOL", "产品策略"),
            DbColumn("account_ams_name", "SYMBOL", "AMS资金账户"),
            DbColumn("prod_ams_name", "SYMBOL", "AMS产品名"),
        ]
        create_table(
            session=session,
            db_name="dfs://infos",
            table_name="prodcode_account",
            columns=prodcode_account_cols,
        )
if __name__ == "__main__":
    initilize_db()


